home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / rfile.arc / RFILE2.C next >
Encoding:
C/C++ Source or Header  |  1987-07-20  |  2.2 KB  |  99 lines

  1. /*   RFILE.C  Random file access functions  */
  2.  
  3. #include "rtbl.h"
  4. #include <FCNTL.H>
  5. #include "sys\stat.h"
  6. #include "io.h"
  7.  
  8. int ropent(filspc,tbl,dat,sz)  /*  open file for random access w/truncation */
  9.  
  10. char *filspc;       /* filespec string */
  11. struct RTBL *tbl;   /* file access table in rtbl.h */
  12. char *dat;          /* record structure, data transfer area */
  13. int sz;             /* sizeof dat */
  14.  
  15. { int port,open();
  16.   long ef,lseek();
  17.  
  18.   port=open(filspc,O_RDWR | O_CREAT | O_BINARY | O_TRUNC,S_IREAD | S_IWRITE );
  19.   if(port<0)  return(1);     /* error return */
  20.   tbl->fprt=port;
  21.   tbl->szdta=sz;
  22.   ef=lseek(port,0L,2);
  23.   ef+=1;
  24.   tbl->hrn=ef/tbl->szdta;
  25.   tbl->dta=dat;
  26.   return(0);     /* no error */
  27. }
  28.  
  29. int ropen(filspc,tbl,dat,sz)  /*  open file for random access  */
  30.  
  31. char *filspc;       /* filespec string */
  32. struct RTBL *tbl;   /* file access table in rtbl.h */
  33. char *dat;          /* record structure, data transfer area */
  34. int sz;             /* sizeof dat */
  35.  
  36. { int port,open();
  37.   long ef,lseek();
  38.  
  39.   port=open(filspc,O_RDWR | O_BINARY,S_IREAD | S_IWRITE );
  40.   if(port<0)  return(1);     /* error return */
  41.   tbl->fprt=port;
  42.   tbl->szdta=sz;
  43.   ef=lseek(port,0L,2);
  44.   ef+=1;
  45.   tbl->hrn=ef/tbl->szdta;
  46.   tbl->dta=dat;
  47.   return(0);     /* no error */
  48. }
  49.  
  50.  
  51. int rfile(ftbl,rec,mode)     /* random file access */
  52.                                   /*   assumes port assigned     */
  53.     struct RTBL *ftbl;     /* file access table */
  54.     int rec;               /* record to read-write */
  55.     char mode;             /*  r = read, w = write  */
  56.  
  57. {
  58.  
  59. int stat,read(),write();
  60. long pos,lseek();
  61.  
  62. stat=0;
  63.  
  64. if(rec > ftbl->hrn) {
  65.     if(mode=='w')
  66.         ftbl->hrn++;
  67.     rec=ftbl->hrn;
  68.     }
  69. rec--;
  70. pos = (long)rec * (long)ftbl->szdta;
  71. lseek(ftbl->fprt,pos,0);
  72. switch(mode) {
  73.     case'r':
  74.         stat=read(ftbl->fprt,ftbl->dta,ftbl->szdta);
  75.         break;
  76.     case'w':
  77.         stat=write(ftbl->fprt,ftbl->dta,ftbl->szdta);
  78.         break;
  79.     }
  80.  
  81. if(stat <= 0)
  82.     return(stat-1);   /* -1=EOF, < (-1) = error */
  83. else
  84.     return(rec+1);
  85. }
  86.  
  87.  
  88.  
  89. int rclose(ctbl)     /* close a random file */
  90.  
  91. struct RTBL *ctbl;     /* file access table */
  92.  
  93. {
  94. int close();
  95.  
  96. close(ctbl->fprt);
  97.  
  98. }
  99.